<template>
  <div class="footer">
    <div class="operate">
      <input type="checkbox" v-model="checkall">
      <span>已选中（{{ checkedTotal }}/{{ todoList.length }}）</span>
    </div>
    <button @click.prevent="clearItem(checkedTotal)">删除已完成事项</button>
  </div>
</template>

<script>
import { computed } from 'vue'
export default {
  name: 'Footer',
  props: {
    todoList: {
      type: Array,
      default: []
    },
    checkAll: {
      type: Function,
      required: true
    },
    clearItem: {
      type: Function,
      required: true
    }
  },
  setup(props) {
    // computed：计算属性
    const checkedTotal = computed(() => {
      return props.todoList.reduce((count, item, index) => count + (item.checked?1:0), 0)
    })

    // computed：计算属性
    const checkall = computed({
      get () {
          return checkedTotal.value
            ? checkedTotal.value > 0 && props.todoList.length === checkedTotal.value
            : checkedTotal.value
      }, set (value) {
        props.checkAll(value)
      }
    })

    return {
      checkedTotal,
      checkall
    }
  }
}
</script>

<style scoped>
  .footer{
    width: calc(100% - 30px);
    padding: 15px;
    margin-top: 10px;
    background-color: #d3e2ff;
    display: inline-flex;
    justify-content: space-between;
    align-items: center;
    /* box-shadow: 0px -2px 3px 0px #8fb3ff; */
  }
  .operate {
    height: 28px;
    line-height: 28px;
    overflow: hidden;
    display: inline-flex;
    align-items: center;
  }
  .operate span{
    font-size: 16px;
    font-weight: 500;
    padding-left: 10px;
  }
</style>